<script>
export default {
  data() {
    return {
      pro_code: "",
    };
  },
  onLaunch: function (e) {
    let timestamp = Date.parse(new Date()) / 1000;
    let logintime = uni.getStorageSync("logintime");
    this.pro_code = e.query.invite_code;
    let p_code = uni.getStorageSync("pro_code");

    if (this.pro_code) {
      // 邀请进入
      // if (pro_code) {
      // 	uni.setStorageSync('pro_code', pro_code);
      // 	return
      // }
      this.proCodeLogin();
    } else {
      // 微信登录
      this.getCode();
    }
  },
  onShow: function () {
    // console.log('App Show')
  },
  onHide: function () {
    // console.log('App Hide')
  },
  methods: {
    getQueryString(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      //#ifdef H5
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      //#endif
      return null;
    },
    // 邀请码进入程序
    async proCodeLogin() {
      let that = this;
      // var inviteCode = that.getQueryString("invite_code");
      var code = that.getQueryString("code");
      let en = window.navigator.userAgent.toLowerCase();
      let url = window.location.href;

      // var fullurl = location.search;
      // var theRequest = new Object();
      // // alert(fullurl)
      // if (fullurl.indexOf("?") != -1) {
      // 	var str = fullurl.substr(1);
      // 	var strs = str.split("&");
      // 	// alert(strs)

      // 	theRequest[strs[0].split("=")[0]] = unescape(strs[0].split("=")[1]);
      // 	var inviteCode = theRequest[strs[0].split("=")[0]]
      // 	alert(inviteCode);
      // }

      // this.code = ''
      // var local = window.location.href // 获取页面url
      // var appid = 'wxf30ba192924d1eaa'
      this.code = this.getUrlCode().code; // 截取code
      if (en.match(/MicroMessenger/i) == "micromessenger") {
        if (this.code == null || this.code === "") {
          // 如果没有code，则去请求

          window.location.href =
            "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
            "wxf30ba192924d1eaa" +
            "&redirect_uri=" +
            encodeURIComponent(url) +
            "&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect";
        } else {
          var code = that.getQueryString("code");
          // alert(code)

          // 获取openid用户信息

          const data = await that.$myRequest({
            url: "user/getOpenid",
            method: "POST",
            data: {
              code,
            },
          });

          // console.log(data)
          let avatarUrl = data.data.data.headimgurl; //用户头像
          let nickname = data.data.data.nickname; // 用户昵称
          let openId = data.data.data.openid; // 用户openid
          let gender = data.data.data.sex; // 用户性别
          let province = data.data.data.province;
          let country = data.data.data.country;
          let city = data.data.data.city;
          let unionId = data.data.data.unionid;

          uni.setStorageSync("openId", openId);
          if (data.statusCode == 200) {
            const datas = await that.$myRequest({
              url: "user/add_users",
              method: "POST",
              data: {
                avatarUrl,
                nickname,
                openId,
                gender,
                province,
                country,
                city,
                unionId,
                inviteCode: that.pro_code,
              },
            });
            // console.log(datas)
            if (datas.data.code == 1) {
              uni.setStorageSync("token", datas.data.data.token);
              uni.setStorageSync("user", datas.data.data);
              that.getuserInfo();
            }
          }
        }
      } else {
        console.log("不在微信 邀请");
      }
    },

    // 非静默授权，第一次有弹框
    async getCode() {
      let that = this;
      let pro_code = that.getQueryString("pro_code");
      var code = that.getQueryString("code");
      let en = window.navigator.userAgent.toLowerCase();
      let url = window.location.href;

      // this.code = ''
      // var local = window.location.href // 获取页面url
      // var appid = 'wxf30ba192924d1eaa'
      this.code = this.getUrlCode().code; // 截取code
      if (en.match(/MicroMessenger/i) == "micromessenger") {
        if (this.code == null || this.code === "") {
          // 如果没有code，则去请求

          window.location.href =
            "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
            "wxf30ba192924d1eaa" +
            "&redirect_uri=" +
            encodeURIComponent(url) +
            "&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect";
        } else {
          var code = that.getQueryString("code");
          // alert(code)

          // 获取openid用户信息

          const data = await that.$myRequest({
            url: "user/getOpenid",
            method: "POST",
            data: {
              code,
            },
          });

          // console.log(data)
          let avatarUrl = data.data.data.headimgurl; //用户头像
          let nickname = data.data.data.nickname; // 用户昵称
          let openId = data.data.data.openid; // 用户openid
          let gender = data.data.data.sex; // 用户性别
          let province = data.data.data.province;
          let country = data.data.data.country;
          let city = data.data.data.city;
          let unionId = data.data.data.unionid;

          uni.setStorageSync("openId", openId);
          if (data.statusCode == 200) {
            const datas = await that.$myRequest({
              url: "user/add_users",
              method: "POST",
              data: {
                avatarUrl,
                nickname,
                openId,
                gender,
                province,
                country,
                city,
                unionId,
                // inviteCode,
              },
            });
            // console.log(datas)
            if (datas.data.code == 1) {
              uni.setStorageSync("token", datas.data.data.token);
              uni.setStorageSync("user", datas.data.data);
              that.getuserInfo();
            }
          }
        }
      } else {
        console.log("不在微信");
      }
    },

    getUrlCode() {
      // 截取url中的code方法
      var url = location.search;
      this.winUrl = url;

      var theRequest = new Object();
      if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
        }
      }
      return theRequest;
    },

    // 获取用户信息
    async getuserInfo() {
      let that = this;
      const res = await that.$myRequest({
        url: "user/getUserInfo",
        method: "GET",
        data: {},
      });
      if (res.data.code == 1) {
        that.userInfo = res.data.data;
        uni.setStorageSync("userInfo", res.data.data);
      }
    },
  },
};
</script>

<style lang="scss">
.navbar {
  display: flex;
  align-items: flex-end;
  background-color: #c52b2e;
  width: 100%;
  height: 90rpx;
  position: fixed;
  z-index: 999 !important;

  // margin-top: 65rpx;
}

.interval {
  padding-top: 108rpx;
}

.search_box {
  display: flex;
  align-items: center;
  // padding-right: 20rpx;
  height: 110rpx;
  line-height: 110rpx;
  width: 100%;
  box-sizing: border-box;
  // padding-top: 10rpx;
  background-color: #c52b2e;
  position: fixed;
  z-index: 999 !important;

  .goback {
    width: 22rpx;
    // margin: 0 0rpx 0 28rpx;
    position: relative;
    left: 28rpx;
  }

  .topicText {
    font-size: 35rpx;
    color: #ffffff;
    margin: 0 auto;
    // padding-left: 70rpx;
    letter-spacing: 2rpx;
    font-weight: 700;
  }

  .search {
    display: flex;
    align-items: center;
    width: 82%;
    background-color: #fff;
    height: 69rpx;
    border-radius: 20px;
    margin: 0 0 0 28rpx;

    .searchImg {
      width: 49rpx;
      height: 43rpx;
      margin-left: 20rpx;
    }

    .searchInp {
      font-size: 30rpx;
      color: #333;
      padding-left: 10rpx;
    }
  }

  .userImg {
    width: 69rpx;
    height: 69rpx;
    margin: 0 20rpx;
  }
}

.worBtn {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 30rpx;
  letter-spacing: 2rpx;
  width: 211rpx;
  height: 69rpx;
  background-color: #c52b2e;
  border-radius: 35rpx;
  position: fixed;
  bottom: 100rpx;
  left: 50%;
  transform: translate(-50%, -50%);
}
uni-tabbar {
  .uni-tabbar {
    box-shadow: 0px -1px 1px 0px rgba(204, 204, 204, 0.5);
    .uni-tabbar-border {
      background-color: transparent !important; // tabBar上边框的颜色
    }
    .uni-tabbar__icon {
      width: 60rpx;
      height: 40rpx;
    }
    .uni-tabbar__label {
      font-weight: bold;
    }
  }
}

/*每个页面公共css */
</style>
